package com.real.operationalcontrols.适配器;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.real.operationalcontrols.R;
import com.real.operationalcontrols.实体类.UserViewInfo;
import com.real.operationalcontrols.工具包.DeleteItemListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ImagesListAdapter extends RecyclerView.Adapter<ImagesListAdapter.ViewHolder> {
    Context context;
    DeleteItemListener itemListener;
    private ArrayList<UserViewInfo> data = new ArrayList<>();

    public ImagesListAdapter(Context context) {
        this.context = context;
    }

    public void setData(ArrayList<UserViewInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setItemListener(DeleteItemListener itemListener) {
        this.itemListener = itemListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_inage_item,
                viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RequestOptions option = new RequestOptions();
        option.error(R.mipmap.ic_iamge_zhanwei);
        Glide.with(context).load(data.get(position).getUrl()).apply(option).into(holder.imgArlm);
        holder.itemView.setOnClickListener(v -> itemListener.setDeleteItemListener(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_arlm)
        ImageView imgArlm;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
